#include "Directionalpad.h"
#include <QMouseEvent>
#include <../RoCore/PublicClass/StyleObject.h>

namespace RO_Robot{

DirectionalPad::DirectionalPad(QWidget *parent)
    : QWidget(parent)
{
    _scale = 1.0;
    _buttons.resize(5);
    _outArcBtn.resize(5);
    _triangle.resize(5);
    _centerButtons.resize(4);

    _triangleSize = 26;

    setMouseTracking(true);
    setMinimumSize(100, 100);
}

RO_Robot::DirectionalPad::~DirectionalPad()
{

}

void DirectionalPad::createButtons()
{
    // upButton-按钮路径
    QPainterPath upButton;
    const qreal scaleX = 0.99; // 水平缩放系数

    upButton.moveTo(33.5434 * scaleX, -37.0789);
    upButton.arcTo(QRectF(-50 * scaleX, -50, 100 * scaleX, 100), 47.87, 84.27);
    upButton.lineTo(-65.4074 * scaleX, -68.9429);
    upButton.arcTo(QRectF(-95 * scaleX, -95, 190 * scaleX, 190), 133.49, -86.98);
    upButton.closeSubpath();

    QPainterPath upOutArc;
    upOutArc.moveTo(-65.4074 * scaleX, -68.9429);
    upOutArc.arcTo(QRectF(-95 * scaleX, -95, 190 * scaleX, 190), 133.49, -86);

    // 添加小正三角形
    double angle = 60.0; // 角度值
    double radians = angle * M_PI / 180.0; // 转换为弧度
    double sinValue = sin(radians);    // 正弦
    double cosValue = cos(radians);    // 余弦
    _triangleWidth = _triangleSize * cosValue; // 正三角形高度
    _triangleHeight = _triangleSize * sinValue;  //正三角形宽度

    // 定义正三角形的路径（原点在三角形中心）
    QPainterPath triangle;
    triangle.moveTo(0, -85); // 顶点
    triangle.lineTo(-_triangleWidth, _triangleHeight - 85); // 左下角
    triangle.lineTo(_triangleWidth, _triangleHeight - 85); // 右下角
    triangle.closeSubpath();

    //添加中心按钮
    QPainterPath outerCircle;
    outerCircle.addEllipse(-43, -43, 86, 86);

    QPainterPath inerCircle;
    inerCircle.addEllipse(-20, -20, 40, 40);

    QPainterPath mRect;
    mRect.addRect(-5, -5, 10, 10);

    QTransform trans;
    // 路径转换
    trans.translate(rect().center().x(), rect().center().y());
    trans.scale(_scale, _scale);
    _buttons[E_Up] = trans.map(upButton);
    _outArcBtn[E_Up] = trans.map(upOutArc);
    _triangle[E_Up] = trans.map(triangle);

    _centerButtons[E_Outer] = trans.map(outerCircle);
    _centerButtons[E_Iner] = trans.map(inerCircle);
    _centerButtons[E_Min_Rect] = trans.map(mRect);

    trans.rotate(-90);
    _buttons[E_LeftRotate] = trans.map(upButton);
    _outArcBtn[E_LeftRotate] = trans.map(upOutArc);
    _triangle[E_LeftRotate] = trans.map(triangle);


    trans.rotate(-90);
    _buttons[E_Down] = trans.map(upButton);
    _outArcBtn[E_Down] = trans.map(upOutArc);
    _triangle[E_Down] = trans.map(triangle);

    trans.rotate(-90);
    _buttons[E_RightRotate] = trans.map(upButton);
    _outArcBtn[E_RightRotate] = trans.map(upOutArc);
    _triangle[E_RightRotate] = trans.map(triangle);
    trans.reset();
}

void DirectionalPad::drawDirectionalButtons(QPainter &painter)
{
    QPen mypen;
    mypen.setColor(QColor(240,242,243));
    mypen.setWidth(4*_scale);
    mypen.setStyle(Qt::SolidLine);
    painter.save();
    painter.setPen(mypen);

    painter.setBrush(isButtonPressed(E_Down) || _mouseMoveBtn == E_Down ? QColor(227,237,254) : QColor(255, 255, 255));
    painter.drawPath(_buttons[E_Down]);

    painter.setBrush(isButtonPressed(E_Up) || _mouseMoveBtn == E_Up ? QColor(227,237,254) : QColor(255, 255, 255));
    painter.drawPath(_buttons[E_Up]);

    // 绘制左旋转按钮
    painter.setBrush(isButtonPressed(E_LeftRotate) || _mouseMoveBtn == E_LeftRotate ? QColor(227,237,254) : QColor(255, 255, 255));
    painter.drawPath(_buttons[E_LeftRotate]);

    // 在左按钮上绘制图标
    QPixmap leftIcon(QString("%1/images/RobotLibrary/LeftRotate.svg").arg(StyleObject::init()->currentSkinDirPath())); // 使用资源文件中的图标
    QRect leftButtonRect = _buttons[E_LeftRotate].boundingRect().toRect(); // 获取按钮区域
    // 计算图标位置（居中）
    QPoint leftIconPos(leftButtonRect.center() - leftIcon.rect().center());
    painter.drawPixmap(leftIconPos, leftIcon);

    // 绘制右旋转按钮
    painter.setBrush(isButtonPressed(E_RightRotate) || _mouseMoveBtn == E_RightRotate ? QColor(227,237,254) : QColor(255, 255, 255));
    painter.drawPath(_buttons[E_RightRotate]);

    // 在右按钮上绘制图标
    QPixmap rightIcon(QString("%1/images/RobotLibrary/RightRotate.svg").arg(StyleObject::init()->currentSkinDirPath())); // 使用资源文件中的图标
    QRect rightButtonRect = _buttons[E_RightRotate].boundingRect().toRect(); // 获取按钮区域
    // 计算图标位置（居中）
    QPoint rightIconPos(rightButtonRect.center() - rightIcon.rect().center());
    painter.drawPixmap(rightIconPos, rightIcon);
    drawDirectionalPressButtons(painter);

    painter.restore();
}

void DirectionalPad::drawDirectionalPressButtons(QPainter &painter)
{
    QPen pen;
    int lineWith=4*_scale;
    if(isButtonPressed(E_Down)){
        painter.save();
        pen.setColor(QColor(240,242,243));
        pen.setWidth(4*_scale);
        pen.setStyle(Qt::SolidLine);
        painter.setPen(pen);
        painter.setBrush(isButtonPressed(E_Down) || _mouseMoveBtn == E_Down ? QColor(227,237,254) : QColor(255, 255, 255));
        painter.drawPath(_buttons[E_Down]);

        pen.setWidth(lineWith);
        pen.setColor(QColor(114,135,196));
        painter.setPen(pen);
        painter.drawPath(_outArcBtn[E_Down]);
        painter.restore();
    }

    if(isButtonPressed(E_Up)){
        painter.save();
        pen.setColor(QColor(240,242,243));
        pen.setWidth(4*_scale);
        pen.setStyle(Qt::SolidLine);
        painter.setBrush(isButtonPressed(E_Up) || _mouseMoveBtn == E_Up ? QColor(227,237,254) : QColor(255, 255, 255));
        painter.drawPath(_buttons[E_Up]);
        pen.setWidth(lineWith);
        pen.setColor(QColor(114,135,196));
        painter.setPen(pen);
        painter.drawPath(_outArcBtn[E_Up]);

        painter.restore();
    }

    if(isButtonPressed(E_LeftRotate)){
        painter.save();

        pen.setColor(QColor(240,242,243));
        pen.setWidth(4*_scale);
        pen.setStyle(Qt::SolidLine);
        painter.setBrush(isButtonPressed(E_LeftRotate) || _mouseMoveBtn == E_LeftRotate ? QColor(227,237,254) : QColor(255, 255, 255));
        painter.drawPath(_buttons[E_LeftRotate]);

        pen.setWidth(lineWith);
        pen.setColor(QColor(114,135,196));
        painter.setPen(pen);
        painter.drawPath(_outArcBtn[E_LeftRotate]);

        // 在左按钮上绘制图标
        QPixmap leftIcon(QString("%1/images/RobotLibrary/LeftRotate.svg").arg(StyleObject::init()->currentSkinDirPath())); // 使用资源文件中的图标
        QRect leftButtonRect = _buttons[E_LeftRotate].boundingRect().toRect(); // 获取按钮区域
        // 计算图标位置（居中）
        QPoint leftIconPos(leftButtonRect.center() - leftIcon.rect().center());
        painter.drawPixmap(leftIconPos, leftIcon);
        painter.restore();
    }

    if(isButtonPressed(E_RightRotate)){
        painter.save();

        pen.setColor(QColor(240,242,243));
        pen.setWidth(4*_scale);
        pen.setStyle(Qt::SolidLine);
        painter.setBrush(isButtonPressed(E_RightRotate) || _mouseMoveBtn == E_RightRotate ? QColor(227,237,254) : QColor(255, 255, 255));
        painter.drawPath(_buttons[E_RightRotate]);

        pen.setWidth(lineWith);
        pen.setColor(QColor(114,135,196));
        painter.setPen(pen);
        painter.drawPath(_outArcBtn[E_RightRotate]);
        // 在右按钮上绘制图标
        QPixmap rightIcon(QString("%1/images/RobotLibrary/RightRotate.svg").arg(StyleObject::init()->currentSkinDirPath())); // 使用资源文件中的图标
        QRect rightButtonRect = _buttons[E_RightRotate].boundingRect().toRect(); // 获取按钮区域
        // 计算图标位置（居中）
        QPoint rightIconPos(rightButtonRect.center() - rightIcon.rect().center());
        painter.drawPixmap(rightIconPos, rightIcon);
        painter.restore();
    }
}

void DirectionalPad::drawTriangle(QPainter &painter)
{
    painter.save();

    painter.setBrush(QColor(87, 127, 212));
    painter.drawPath(_triangle[E_Up]);
    painter.setBrush(QColor(87, 127, 212));
    painter.drawPath(_triangle[E_Down]);
    //drawPressTriangles(painter);
    painter.restore();
}

void DirectionalPad::drawPressTriangles(QPainter &painter)
{
    qreal offset1 = -12 * _scale;
    qreal offset2 = 6 * _scale;
    // if(isButtonPressed(E_RightRotate)){
    //     painter.save();
    //     painter.setBrush(QColor(187,206,245));
    //     painter.translate(offset1, 0);
    //     painter.drawPath(_triangle[E_RightRotate]);

    //     painter.setBrush(QColor(142,161,217));
    //     painter.translate(-offset1 - offset2, 0); // 从第一个位置移动到第二个位置
    //     painter.drawPath(_triangle[E_RightRotate]);

    //     painter.setBrush(QColor(116,144,199));
    //     painter.translate(offset2, 0); // 从第二个位置移动到第三个位置
    //     painter.drawPath(_triangle[E_RightRotate]);
    //     painter.restore();
    // }

    if(isButtonPressed(E_Down)){
        painter.save();
        painter.setBrush(QColor(187,206,245));
        painter.translate(0,offset1);
        painter.drawPath(_triangle[E_Down]);

        painter.setBrush(QColor(142,161,217));
        painter.translate(0,-offset1 - offset2);
        painter.drawPath(_triangle[E_Down]);

        painter.setBrush(QColor(116,144,199));
        painter.translate(0,offset2);
        painter.drawPath(_triangle[E_Down]);

        painter.restore();
    }

    // if(isButtonPressed(E_LeftRotate)){
    //     painter.save();
    //     painter.setBrush(QColor(187,206,245));
    //     painter.translate(-offset1,0);
    //     painter.drawPath(_triangle[E_LeftRotate]);

    //     painter.setBrush(QColor(142,161,217));
    //     painter.translate(offset1 + offset2,0);
    //     painter.drawPath(_triangle[E_LeftRotate]);

    //     painter.setBrush(QColor(116,144,199));
    //     painter.translate(-offset2,0);
    //     painter.drawPath(_triangle[E_LeftRotate]);

    //     painter.restore();
    // }

    if(isButtonPressed(E_Up)){
        painter.save();
        painter.setBrush(QColor(187,206,245));
        painter.translate(0,-offset1);
        painter.drawPath(_triangle[E_Up]);

        painter.setBrush(QColor(142,161,217));
        painter.translate(0,offset1 + offset2);
        painter.drawPath(_triangle[E_Up]);

        painter.setBrush(QColor(116,144,199));
        painter.translate(0,-offset2);
        painter.drawPath(_triangle[E_Up]);

        painter.restore();
    }

}

void DirectionalPad::drawCenterButtons(QPainter &painter)
{
    painter.save();
    //绘制外圆
    painter.setBrush(isButtonPressed(E_Home) || _mouseMoveBtn == E_Home? QColor(227,237,254) : QColor(255, 255, 255));
    painter.drawPath(_centerButtons[E_Outer]);

    // 绘制内圆
    painter.setBrush(QColor(87, 127, 212));
    painter.drawPath(_centerButtons[E_Iner]);

    // 绘制中心矩形
    painter.setBrush(QColor(255,255,255));
    painter.drawPath(_centerButtons[E_Min_Rect]);

    painter.restore();
}

bool DirectionalPad::isButtonPressed(Direction dir) const
{
    if (_mousePressedBtn != E_None) {
        return _mousePressedBtn == dir;
    }
     return _keyboardPressedBtn.contains(dir);
}

DirectionalPad::Direction DirectionalPad::getDirectionFromPoint(const QPointF &point)
{
    if (_centerButtons[E_Outer].contains(point)) {
        return E_Home;
    } else if (_buttons[E_Up].contains(point)) {
        return E_Up;
    } else if (_buttons[E_Down].contains(point)) {
        return E_Down;
    } else if (_buttons[E_LeftRotate].contains(point)) {
        return E_LeftRotate;
    } else if (_buttons[E_RightRotate].contains(point)) {
        return E_RightRotate;
    }
    return E_None;
}

void DirectionalPad::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);

    drawDirectionalButtons(painter);
    drawTriangle(painter);
    drawCenterButtons(painter);
    update();
}

void DirectionalPad::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    int edge = qMin(size().width(), size().height());
    _scale = edge / 200.0;
    createButtons();
}

void DirectionalPad::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    _mousePressedBtn = E_None;
    _mouseMoveBtn = E_None;
    QPointF pos = event->position();
    Direction releasebutton = getDirectionFromPoint(pos);

    switch (releasebutton) {
    case E_LeftRotate:
    {
        emit sigReleaseDirectionLeftRotate("A");
        break;
    }
    case E_RightRotate:
    {
        emit sigReleaseDirectionRightRotate("D");
        break;
    }
    case E_Up:
    {
        emit sigReleaseDirectionUp("W");
        break;
    }
    case E_Down:
    {
        emit sigReleaseDirectionDown("S");
        break;
    }
    case E_Home:
    {
        emit sigReleasePause("Q");
        break;
    }
    default:
        break;
    }
    update();
}

void DirectionalPad::mouseMoveEvent(QMouseEvent *event)
{
    QPointF pos = event->position();
    Direction button = getDirectionFromPoint(pos);

    _mouseMoveBtn = button;
    update();
}

void RO_Robot::DirectionalPad::leaveEvent(QEvent *event)
{
    _mouseMoveBtn = E_None;
    update();
}


void DirectionalPad::mousePressEvent(QMouseEvent *event)
{
    QPointF pos = event->position();
    Direction button = getDirectionFromPoint(pos);

    if (button != E_None) {
        _mousePressedBtn = button;
        update();
    }

    switch (_mousePressedBtn) {
    case E_LeftRotate:
    {
        emit sigDirectionLeftRotate("A");
        break;
    }
    case E_RightRotate:
    {
        emit sigDirectionRightRotate("D");
        break;
    }
    case E_Up:
    {
        emit sigDirectionUp("W");
        break;
    }
    case E_Down:
    {
        emit sigDirectionDown("S");
        break;
    }
    case E_Home:
    {
        emit sigPause("Q");
        break;
    }
    default:
        break;
    }
}

void DirectionalPad::slotKeyChecked(const QString &key)
{
    Direction newDirection = E_None;
    if ("W" == key){
        newDirection = E_Up;
    }else if ("S" == key){
        newDirection = E_Down;
    }else if ("A" == key){
        newDirection = E_LeftRotate;
    }else if ("D" == key){
        newDirection = E_RightRotate;
    }else if ("Q" == key){
        newDirection = E_Home;
    }else{
        newDirection = E_None;
    }

    if (newDirection != E_None) {
        _keyboardPressedBtn.insert(newDirection);
        update();
    }
}

void DirectionalPad::slotKeyUnChecked(const QString &key)
{
    Direction releasedDirection = E_None;

    if ("W" == key){
        releasedDirection = E_Up;
    }else if ("S" == key){
        releasedDirection = E_Down;
    }else if ("A" == key){
        releasedDirection = E_LeftRotate;
    }else if ("D" == key){
        releasedDirection = E_RightRotate;
    }else if ("Q" == key){
        releasedDirection = E_Home;
    }else{
        releasedDirection = E_None;
    }

    if (releasedDirection != E_None) {
        _keyboardPressedBtn.remove(releasedDirection);
        update();
    }
}

/*void DirectionalPad::keyPressEvent(QKeyEvent *event)
{
    Direction newDirection = E_None;
    switch(event->key()) {
    case Qt::Key_W:
    {
        newDirection = E_Up;
        emit sigDirectionUp("W");
        break;
    }
    case Qt::Key_S:
    {
        newDirection = E_Down;
        emit sigDirectionDown();
        break;
    }
    case Qt::Key_E:
    {
        newDirection = E_LeftRotate;
        emit sigDirectionLeftRotate();
        break;
    }
    case Qt::Key_R:
    {
        newDirection = E_RightRotate;
        emit sigDirectionRightRotate();
        break;
    }
    case Qt::Key_Q:
    {
        newDirection = E_Home;
        emit sigPause();
        break;
    }
    default:
        QWidget::keyPressEvent(event);
        return;
    }

    if (newDirection != E_None) {
        _keyboardPressedBtn.insert(newDirection);
        update();
    }

    event->accept();
}

void DirectionalPad::keyReleaseEvent(QKeyEvent *event)
{
    Direction releasedDirection = E_None;
    switch(event->key()) {
    case Qt::Key_W:
    {
        releasedDirection = E_Up;
        emit sigReleaseDirectionUp("W");
        break;
    }
    case Qt::Key_S:
    {
        releasedDirection = E_Down;
        emit sigReleaseDirectionDown();
        break;
    }
    case Qt::Key_E:
    {
        releasedDirection = E_LeftRotate;
        emit sigReleaseDirectionLeftRotate();
        break;
    }
    case Qt::Key_R:
    {
        releasedDirection = E_RightRotate;
        emit sigReleaseDirectionRightRotate();
        break;
    }
    case Qt::Key_Q:
    {
        releasedDirection = E_Home;
        emit sigReleasePause();
        break;
    }
    default:
        QWidget::keyReleaseEvent(event);
        return;
    }

    if (releasedDirection != E_None) {
        _keyboardPressedBtn.remove(releasedDirection);
        update();
    }
    QWidget::keyReleaseEvent(event);
}*/
}
